package com.maoye.ossp.sys.base.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.sys.base.entity.BerthMaindata;
import com.maoye.ossp.sys.base.service.BerthMaindataService;

/**
 * 铺位主数据
 * 
 * @author liuw
 * @data 2014年2月25日
 */
@Controller()
@RequestMapping(value = "sys/base/berthmaindata")
public class BerthMaindataController extends BaseController {
	
	@Resource
	BerthMaindataService service;
	
	
	
	@RequestMapping(value="create")
	public String create(Model model){
		BerthMaindata data = new BerthMaindata();
		data.setId(idGenerator.getUId());
		data.setArea(new BigDecimal("1000"));
		data.setSiteid("1111");
		data.setFloor("5");
		data.setValidfro(new Date());
		data.setValidto(new Date());
		data.setSnunr("AAAA");
		service.create(data);
		return "";
	}
	
	@RequestMapping(value="get")
	public String get(Long id, Model model,HttpServletRequest request){
		BerthMaindata berthMaindata = service.get(id);
		model.addAttribute("brethMaindata", berthMaindata);
		return resultPage("/sys/base/Berth/view", model, request);
	}
	
	@RequestMapping(value="ajaxGet")
	@ResponseBody
	public Map<String, Object> ajaxGet(Long id, Model model){
		Map<String, Object> map = new HashMap<String, Object>();
		BerthMaindata berthMaindata = service.get(id);
		map.put("berthMaindata", berthMaindata);
		return map;
	}
	
	@RequestMapping(value="queryByIds")
	@ResponseBody
	public Map<String, Object> queryByIds(String mechIds, Model model){
		Map<String, Object> map = new HashMap<String, Object>();
		List<BerthMaindata> list = service.getByMechIds(mechIds);
		String zNodes = "[";
		if (list != null && !list.isEmpty()) {
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i) != null) {
					zNodes += "{id:\"" + list.get(i).getMechId()
							+ "\", pId:0, name:\"" + list.get(i).getMechId()
							+ "\"}";
					if (i < list.size() - 1) {
						zNodes += ",";
					}
				}
			}
		}
		zNodes += "]";
		map.put("zNodes", zNodes);
		map.put("list", list);
		return map;
	}
	
	@RequestMapping(value="getList")
	public String getList(BerthMaindata berthMaindata, Model model,HttpServletRequest request){
		QueryFilter queryFilter = getQuerFilter(request,berthMaindata);
		
		Page<BerthMaindata> page = service.find(queryFilter);
		model.addAttribute("page", page);
		return resultPage("/sys/base/Berth/list", model, request);
	}
	
	@RequestMapping(value="getBerth")
	@ResponseBody
	public Map<String, Object> getBerth(BerthMaindata berthMaindata, Model model){
		Map<String, Object> map = new HashMap<String, Object>();
		List<BerthMaindata> list = service.getByFloor(berthMaindata);
		map.put("list", list);
		return map;
	}
	
	//test
	@RequestMapping(value="selectPage")
	public String selectPage(BerthMaindata berthMaindata,Model model){
		List<BerthMaindata> list = service.getByFloor(berthMaindata);
		model.addAttribute("list", list);
		return "/sys/base/Berth/selectPage";
	}
	
	@RequestMapping(value="showModal")
	public String showModal(BerthMaindata berthMaindata,Model model){
		List<BerthMaindata> list = service.getByFloor(berthMaindata);
		model.addAttribute("list", list);
		return "/sys/base/Berth/modalBerth";
	}
}
